package com.itqf.service.impl;

import com.itqf.bean.DtsUserExample;
import com.itqf.mapper.DtsUserMapper;
import com.itqf.service.DtsUserService;
import com.itqf.vo.DayStatis;
import com.itqf.vo.OrderAmtsVo;
import com.itqf.vo.UserOrderCntVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * projectName: gp_manage
 *
 * @author: 赵伟风
 * time: 2021/8/18 18:05
 * description:
 */
@Service
public class DtsUserServiceImpl implements DtsUserService {


    @Autowired
    private DtsUserMapper dtsUserMapper;

    /**
     * 查询用户数量
     *
     * @return
     */
    @Override
    public Long findUserTotal() {


        DtsUserExample example = new DtsUserExample();

        //非禁用（status == 0） 非逻辑删除
        DtsUserExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo((byte) 0);
        criteria.andDeletedEqualTo(false);

        Long userTotal  = dtsUserMapper.countByExample(example);

        return userTotal;

    }

    /**
     * 用户和订单的日增长量
     *
     * @param orderAmtsVo
     * @return
     */
    @Override
    public UserOrderCntVo findUserAndOrderData(OrderAmtsVo orderAmtsVo) {


        //1.查询用户的增长量
        List<DayStatis> dayStatisList = dtsUserMapper.selectUserData();

        //2.订单和用户整合

        Set<String> dayData = new HashSet<>();


        Map<String,Integer> userCntMap = new HashMap<>();
        Map<String,Integer> orderCntMap = new HashMap<>();


        for (DayStatis dayStatis : dayStatisList) {
            dayData.add(dayStatis.getDayStr());
            userCntMap.put(dayStatis.getDayStr(),dayStatis.getCnts());
        }

        int index = 0 ;
        for (String dayDatum : orderAmtsVo.getDayData()) {
            dayData.add(dayDatum);
            int order = orderAmtsVo.getOrderCntData()[index];
            orderCntMap.put(dayDatum,order);
            index++;
        }

        int [] userCnt = new int [dayData.size()];
        int [] orderCnt = new int [dayData.size()];

        //遍历时间的并集
        index = 0;
        for (String dayDatum : dayData) {
            userCnt[index] = userCntMap.getOrDefault(dayDatum,0);
            orderCnt[index] = orderCntMap.getOrDefault(dayDatum, 0);
            index++;
        }

        //3.封装vo返回
        UserOrderCntVo userOrderCntVo = new UserOrderCntVo();

        userOrderCntVo.setOrderCnt(orderCnt);
        userOrderCntVo.setUserCnt(userCnt);

        userOrderCntVo.setDayData(dayData.toArray(new String [0]));


        return userOrderCntVo;
    }
}
